package com.mission.schedule.activity;

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.mission.schedule.R;
import com.mission.schedule.annotation.ViewResId;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.MyMessageBackBean;
import com.mission.schedule.bean.MyMessageBean;
import com.mission.schedule.bean.SuccessOrFailBean;
import com.mission.schedule.circleview.CircularImage;
import com.mission.schedule.constants.Const;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.cutimage.Crop;
import com.mission.schedule.service.DownImageService;
import com.mission.schedule.utils.AfterPermissionGranted;
import com.mission.schedule.utils.EasyPermissions;
import com.mission.schedule.utils.ImageCutUtils;
import com.mission.schedule.utils.ImageUtils;
import com.mission.schedule.utils.NetUtil;
import com.mission.schedule.utils.NetUtil.NetWorkState;
import com.mission.schedule.utils.PathFromUriUtils;
import com.mission.schedule.utils.PhotoActionHelper;
import com.mission.schedule.utils.ProgressUtil;
import com.mission.schedule.utils.RxPhotoUtils;
import com.mission.schedule.utils.SharedPrefUtil;
import com.mission.schedule.utils.StringUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.yalantis.ucrop.UCrop;

import java.io.File;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class PersonMessageActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {

    @ViewResId(id = R.id.top_ll_back)
    private LinearLayout top_ll_back;// 返回
    @ViewResId(id = R.id.image_img)
    private CircularImage image_img;// 个人头像
    // @ViewResId(id = R.id.image_linear)
    // private RelativeLayout image_linear;// 头像
    // @ViewResId(id = R.id.state_re)
    // private RelativeLayout state_re;
    @ViewResId(id = R.id.nicheng_tv)
    private TextView nicheng_tv;
    @ViewResId(id = R.id.userID_tv)
    private TextView userID_tv;
    @ViewResId(id = R.id.useremail_tv)
    private TextView useremail_tv;
    @ViewResId(id = R.id.userphone_tv)
    private TextView userphone_tv;
    @ViewResId(id = R.id.state_tv)
    private TextView state_tv;
    @ViewResId(id = R.id.photo_rl)
    private RelativeLayout photo_rl;
    @ViewResId(id = R.id.nicheng_ll)
    private RelativeLayout nicheng_ll;
    @ViewResId(id = R.id.email_ll)
    private RelativeLayout email_ll;
    @ViewResId(id = R.id.telephone_ll)
    private RelativeLayout telephone_ll;
    @ViewResId(id = R.id.alterpassword_re)
    private RelativeLayout alterpassword_re;
    @ViewResId(id = R.id.remark_rl)
    private RelativeLayout remark_rl;
    @ViewResId(id = R.id.remark_tv)
    private TextView remark_tv;

    Context context;
    // private final static int STATE_CHOOSE = 1;// 状态选择
    private final static int STATE_NAME = 10;// 修改名称
    private final static int STATE_EMAIL = 11;// 修改邮箱
    private final static int STATE_TELEPHONE = 12;// 修改手机号
    private final static int STATE_PASSWORD = 13;// 修改密码
    private final static int STATE_REMARK = 14;//修改签名

    SharedPrefUtil sharedPrefUtil = null;
    String userId;
    String name;
    String email;
    String telephone;
    String U_ACC_NO;
    String remark;
    Bitmap bitmap = null;

    private static final int REQUEST_CAMERA_CODE = 6;// 相机选择器
    private static final int REQUEST_ALBUM_CODE = 7;// 相册选择器
    private static final int REQUEST_SAVE_CODE = 8;// 保存选择
    private String realPath;// 原图路径
    private String clipPath = "";// 裁剪过后的图片路径
    private boolean isRealImg;// 判断是否是相机拍摄的图
    private ProgressUtil progressUtil = new ProgressUtil();
    String paths;
    private String mOutputPath;
    private String mDemoPath;
    private DisplayImageOptions options; // DisplayImageOptions是用于设置图片显示的类
    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
    private ImageLoader imageLoader;
    boolean permissionFlag = false;

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_personmessageactivity);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        context = this;
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        options = new DisplayImageOptions.Builder()
                .showStubImage(R.mipmap.b1)
                .showImageForEmptyUri(R.mipmap.b1)
                .showImageOnFail(R.mipmap.b1).cacheInMemory(true)
                .cacheOnDisc(true).cacheInMemory(true)
                .bitmapConfig(Bitmap.Config.RGB_565) // 设置图片的解码类型
                .build();
        imageLoader = ImageLoader.getInstance();
        String image_head = sharedPrefUtil.getString(context,
                ShareFile.USERFILE, ShareFile.USERPHOTOPATH, "");
        String imageUrl = URLConstants.图片 + image_head + "&imageType=2&imageSizeType=3";
        imageLoader.displayImage(imageUrl, image_img, options, animateFirstListener);
//        FileUtils.loadRoundHeadImg(this, ParameterUtil.userHeadImg, image_img,
//                imageUrl);
        userId = sharedPrefUtil.getString(context, ShareFile.USERFILE,
                ShareFile.USERID, "");
        U_ACC_NO = sharedPrefUtil.getString(context, ShareFile.USERFILE,
                ShareFile.U_ACC_NO, "");
        userID_tv.setText(U_ACC_NO);
        name = sharedPrefUtil.getString(context, ShareFile.USERFILE,
                ShareFile.USERNAME, "");
        nicheng_tv.setText(name);
        email = sharedPrefUtil.getString(context, ShareFile.USERFILE,
                ShareFile.USEREMAIL, "");
        telephone = sharedPrefUtil.getString(context, ShareFile.USERFILE,
                ShareFile.TELEPHONE, "");
        remark = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.PERSONREMARK, "");
        if (!"".equals(StringUtils.getIsStringEqulesNull(email))) {
            useremail_tv.setText(email);
        } else {

        }
        if (!"".equals(StringUtils.getIsStringEqulesNull(telephone))) {
            userphone_tv.setText(telephone);
        } else {
            userphone_tv.setText("");
        }
        if (!"".equals(StringUtils.getIsStringEqulesNull(remark))) {
            remark_tv.setText(remark);
        } else {
            remark_tv.setText("");
        }
    }

    @Override
    protected void setAdapter() {
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = null;
            switch (v.getId()) {
                case R.id.top_ll_back:
                    intent = new Intent();
                    setResult(999, intent);
                    finish();
                    break;
                case R.id.image_img:
                    checkPhonePermission();
                    if (autoFag) {
                        Crop.pickImage(PersonMessageActivity.this);
                    } else {
                        Toast.makeText(context, "权限已禁止访问!", Toast.LENGTH_LONG).show();
                    }
//				ImageCutTools.getInstance().selectPicture(
//						PersonMessageActivity.this);
                    // isRealImg = false;// 不是相机拍摄的图
                    // ImageCutUtils.openLocalImage(PersonMessageActivity.this);
                    // dialogShow();
                    break;
                case R.id.photo_rl:// 修改头像
                    checkPhonePermission();
                    // dialogShow();
                    // isRealImg = false;// 不是相机拍摄的图
                    // ImageCutUtils.openLocalImage(PersonMessageActivity.this);
//				ImageCutTools.getInstance().selectPicture(
//						PersonMessageActivity.this);
                    if (autoFag) {
//                        Crop.pickImage(PersonMessageActivity.this);
                        Intent intentc = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        intentc.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                        startActivityForResult(intentc, 5002);
                    } else {
                        Toast.makeText(context, "权限已禁止访问!", Toast.LENGTH_LONG).show();
                    }
                    break;
                case R.id.alterpassword_re:// 修改密码
                    intent = new Intent(context, AlterPasswordActivity.class);
                    startActivityForResult(intent, STATE_PASSWORD);
                    break;
                case R.id.email_ll:// 修改邮箱
                    intent = new Intent(context, AlterEMailActivity.class);
                    startActivityForResult(intent, STATE_EMAIL);
                    break;
                case R.id.telephone_ll:// 修改手机号
                    intent = new Intent(context, AlterTelephoneActivity.class);
                    startActivityForResult(intent, STATE_TELEPHONE);
                    break;
                // case R.id.state_re:
                // intent = new Intent(context, StateActivity.class);
                // intent.putExtra("statename", state_tv.getText().toString());
                // startActivityForResult(intent, STATE_CHOOSE);
                // break;
                case R.id.nicheng_ll:// 修改昵称
                    intent = new Intent(context, AlterPersonNameActivity.class);
                    intent.putExtra("name", nicheng_tv.getText().toString().trim());
                    startActivityForResult(intent, STATE_NAME);
                    break;
                case R.id.remark_rl://修改签名
                    intent = new Intent(context, AlterPersonTagActivity.class);
                    intent.putExtra("tag", remark_tv.getText().toString());
                    startActivityForResult(intent, STATE_REMARK);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void setListener() {
        top_ll_back.setOnClickListener(onClickListener);
        image_img.setOnClickListener(onClickListener);
        // image_linear.setOnClickListener(onClickListener);
        alterpassword_re.setOnClickListener(onClickListener);
        // state_re.setOnClickListener(onClickListener);
        nicheng_ll.setOnClickListener(onClickListener);
        photo_rl.setOnClickListener(onClickListener);
        email_ll.setOnClickListener(onClickListener);
        telephone_ll.setOnClickListener(onClickListener);
        remark_rl.setOnClickListener(onClickListener);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Uri uri = null;
        // if (STATE_CHOOSE == requestCode) {
        // if (resultCode == Activity.RESULT_OK) {
        // String state = data.getStringExtra("state");
        // state_tv.setText(state);
        // }
        // } else
        if (STATE_NAME == requestCode) {
            if (resultCode == Activity.RESULT_OK) {
                String nicheng = data.getStringExtra("name");
                nicheng_tv.setText(nicheng);
            }
        } else if (STATE_EMAIL == requestCode) {
            if (resultCode == Activity.RESULT_OK) {
                String email = data.getStringExtra("email");
                useremail_tv.setText(email);
            }
        } else if (STATE_TELEPHONE == requestCode) {
            if (resultCode == Activity.RESULT_OK) {
                String telephone = data.getStringExtra("telephone");
                userphone_tv.setText(telephone);
            }
        } else if (STATE_REMARK == requestCode) {
            if (resultCode == Activity.RESULT_OK) {
                String tag = data.getStringExtra("tag");
                remark_tv.setText(tag);
            }
        }

        if (resultCode == Activity.RESULT_OK) {
            if (data != null) {
                String fileName = "HeaderImage_" + String.valueOf(System.currentTimeMillis()) + ".png";

                Uri selectImageUri = data.getData();

                if (selectImageUri != null) {
                    Uri mDestinationUri = Uri.fromFile(new File(getCacheDir(), fileName));
                    UCrop.of(selectImageUri, mDestinationUri).withMaxResultSize(200, 200).withAspectRatio(1, 1).start(PersonMessageActivity.this);
                }


                if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
                    final Uri resultUri = UCrop.getOutput(data);
                    String path = RxPhotoUtils.getRealFilePath(context, resultUri);
                    if (path != null) {
                        paths = path;
                        Bitmap bitmap = BitmapFactory.decodeFile(paths);
                        if (bitmap != null) {
                            image_img.setImageBitmap(bitmap);
                        }
                        uploadJvBaoContent();
                    }
                } else if (resultCode == UCrop.RESULT_ERROR) {
                    Toast.makeText(context, "图片裁剪失败", Toast.LENGTH_SHORT).show();
                }
            }
        }

//        if (resultCode == Activity.RESULT_OK) {
//            if (requestCode == Crop.REQUEST_PICK) {
//                File temp = new File(Environment.getExternalStorageDirectory()
//                        .getPath() + "/yourAppCacheFolder/");// 自已缓存文件夹
//                if (!temp.exists()) {
//                    temp.mkdir();
//                }
//                String fileName = "Temp_" + String.valueOf(System.currentTimeMillis()) + ".png";
//                File cropFile = new File(temp, fileName);
//                Uri outputUri = Uri.fromFile(cropFile);
//                mOutputPath = outputUri.getPath();
//                mDemoPath = PathFromUriUtils.getPath(context, data.getData());
//                PhotoActionHelper.clipImage(this, PersonPhotoClipActivity.class).input(mDemoPath).output(mOutputPath)
//                        .requestCode(Const.REQUEST_CLIP_IMAGE).start();
////                beginCrop( data.getData());
//            }
//            if (data != null
//                    && (requestCode == Const.REQUEST_CLIP_IMAGE || requestCode == Const.REQUEST_TAKE_PHOTO)) {
//                String path = PhotoActionHelper.getOutputPath(data);
//                if (path != null) {
//                    bitmap = BitmapFactory.decodeFile(path);
//                    paths = path;
//                    if (bitmap != null) {
//                        if (NetUtil.getConnectState(context) != NetWorkState.NONE) {
//                            uploadJvBaoContent();
//                        } else {
//                            alertFailDialog(0);
//                        }
//                    } else {
//                        alertFailDialog(1);
//                    }
//                }
//            }
//        }

    }

    private void dialogShow() {
        Dialog dialog = new Dialog(this, R.style.dialog_translucent);
        Window window = dialog.getWindow();
        android.view.WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        params.alpha = 0.92f;
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        window.setAttributes(params);// 设置生效

        LayoutInflater fac = LayoutInflater.from(this);
        View more_pop_menu = fac.inflate(R.layout.dialog_pictureorigin, null);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(more_pop_menu);
        params.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = getWindowManager().getDefaultDisplay().getWidth() - 30;
        dialog.show();

        new MyClickClass(more_pop_menu, dialog);
    }

    class MyClickClass implements OnClickListener {

        private Dialog dialog;
        private Button dialog_button_photo;
        private Button dialog_button_album;
        private Button dialog_button_cancel;

        public MyClickClass(View view, Dialog dialog) {
            this.dialog = dialog;
            dialog_button_photo = (Button) view
                    .findViewById(R.id.dialog_button_photo);
            dialog_button_photo.setOnClickListener(this);
            dialog_button_album = (Button) view
                    .findViewById(R.id.dialog_button_album);
            dialog_button_album.setOnClickListener(this);
            dialog_button_cancel = (Button) view
                    .findViewById(R.id.dialog_button_cancel);
            dialog_button_cancel.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            // Intent intent = null;
            switch (v.getId()) {
                case R.id.dialog_button_photo:
                    isRealImg = true;// 相机拍摄的图
                    // intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    // intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new
                    // File(
                    // Environment.getExternalStorageDirectory(), "temp.jpg")));
                    // startActivityForResult(intent, REQUEST_CAMERA_CODE);
                    ImageCutUtils.openCameraImage(PersonMessageActivity.this);
                    break;

                case R.id.dialog_button_album:
                    // Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT);
                    // // "android.intent.action.GET_CONTENT"
                    // innerIntent.setType("image/*"); // 查看类型
                    // startActivityForResult(innerIntent, REQUEST_ALBUM_CODE);
                    isRealImg = false;// 不是相机拍摄的图
                    ImageCutUtils.openLocalImage(PersonMessageActivity.this);

                    break;

            }
            dialog.dismiss();
        }
    }

    /* 根据uri返回文件路径 */
    public String getPath(Uri uri) {
        String[] proj = {MediaStore.Images.Media.DATA};
        // 好像是android多媒体数据库的封装接口，具体的看Android文档
        Cursor cursor = managedQuery(uri, proj, null, null, null);
        // 按我个人理解 这个是获得用户选择的图片的索引值
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        // 将光标移至开头 ，这个很重要，不小心很容易引起越界
        cursor.moveToFirst();
        // 最后根据索引值获取图片路径
        return cursor.getString(column_index);
    }

    public final static Bitmap lessenUriImage(String path) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回 bm 为空
        options.inJustDecodeBounds = false; // 缩放比。由于是固定比例缩放，只用高或者宽其中一个数据进行计算即可
        int be = (int) (options.outHeight / (float) 320);
        if (be <= 0)
            be = 1;
        options.inSampleSize = be; // 重新读入图片，注意此时已经把 options.inJustDecodeBounds
        // 设回 false 了
        bitmap = BitmapFactory.decodeFile(path, options);
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        System.out.println(w + " " + h); // after zoom
        return bitmap;
    }

    private void uploadJvBaoContent() {
        if (NetUtil.getConnectState(this) == NetWorkState.NONE) {
            Toast.makeText(context, "网络异常，请检查网络！", Toast.LENGTH_SHORT).show();
        } else {
            HttpUtils httpUtils = new HttpUtils(10000);
            httpUtils.configCurrentHttpCacheExpiry(1000);
            RequestParams params = new RequestParams();
            params.addBodyParameter("uploadImage", new File(paths));
            params.addBodyParameter("uid", userId);
            httpUtils.send(HttpMethod.POST, URLConstants.修改个人头像, params,
                    new RequestCallBack<String>() {

                        @Override
                        public void onFailure(HttpException e, String msg) {
                            progressUtil.dismiss();
                            Toast.makeText(context, "上传失败！", Toast.LENGTH_SHORT)
                                    .show();
                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            progressUtil.dismiss();
                            if (!TextUtils.isEmpty(responseInfo.result)) {
                                Gson gson = new Gson();
                                SuccessOrFailBean bean;
                                try {
                                    bean = gson.fromJson(responseInfo.result,
                                            SuccessOrFailBean.class);
                                    if (bean.status == 0) {
                                        image_img.setImageBitmap(ImageUtils.getRoundBitmap(context, bitmap));
                                        setResult(1);
                                        sharedPrefUtil.putString(context,
                                                ShareFile.USERFILE,
                                                ShareFile.TOUXIANGSTATE, "0");
//                                        MyMessageAsync(URLConstants.查询个人信息 + "?uid=" + userId);
                                        if (!"头像修改成功".equals(bean.message)) {
                                            sharedPrefUtil.putString(context,
                                                    ShareFile.USERFILE,
                                                    ShareFile.USERPHOTOPATH,
                                                    bean.message);

                                            String imageUrl = URLConstants.图片 + bean.message + "&imageType=2&imageSizeType=3";

                                            String dicr = Environment.getExternalStorageDirectory() + "/DCIM/";
                                            File outfile = new File(dicr, "TimeApp");
                                            // 如果文件不存在，则创建一个新文件
                                            if (!outfile.isDirectory()) {
                                                try {
                                                    outfile.mkdir();
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                            }

                                            Bitmap bitmapHead = BitmapFactory.decodeFile(outfile + "/" + bean.message.replace('/', '0'));

                                            if (bitmapHead == null) {
                                                imageLoader.displayImage(imageUrl, image_img, options, animateFirstListener);
                                                Intent intent = new Intent(context, DownImageService.class);
                                                intent.setAction("downimage");
                                                intent.putExtra("url", imageUrl);
                                                intent.putExtra("name", bean.message.replace('/', '0'));
                                                startService(intent);
                                            }
                                        }
                                    } else {
                                        alertFailDialog(1);
                                    }
                                } catch (JsonSyntaxException e) {
                                    e.printStackTrace();
                                }
                            } else {
                            }
                        }
                    });

        }
    }

    private void alertFailDialog(int type) {
        final AlertDialog builder = new AlertDialog.Builder(context).create();
        builder.show();
        Window window = builder.getWindow();
        android.view.WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        params.alpha = 0.92f;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);// 设置生效
        window.setGravity(Gravity.CENTER);
        window.setContentView(R.layout.dialog_alert_ok);
        TextView delete_ok = (TextView) window.findViewById(R.id.delete_ok);
        TextView delete_tv = (TextView) window.findViewById(R.id.delete_tv);
        if (type == 0) {
            delete_tv.setText("请检查您的网络！");
        } else {
            delete_tv.setText("头像上传失败！");
        }
        delete_ok.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                builder.cancel();
            }
        });

    }

    private void MyMessageAsync(String path) {
        StringRequest request = new StringRequest(Method.GET, path,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String result) {
                        if (!TextUtils.isEmpty(result)) {
                            Gson gson = new Gson();
                            try {
                                MyMessageBackBean backBean = gson.fromJson(
                                        result, MyMessageBackBean.class);
                                if (backBean.status == 0) {
                                    MyMessageBean bean = null;
                                    if (backBean.list != null
                                            && backBean.list.size() > 0) {
                                        for (int i = 0; i < backBean.list
                                                .size(); i++) {
                                            bean = backBean.list.get(i);
                                        }
                                        sharedPrefUtil.putString(context,
                                                ShareFile.USERFILE,
                                                ShareFile.USERBACKGROUNDPATH,
                                                bean.uBackgroundImage);
                                        sharedPrefUtil.putString(context,
                                                ShareFile.USERFILE,
                                                ShareFile.USEREMAIL,
                                                bean.uEmail);
                                        sharedPrefUtil.putString(context,
                                                ShareFile.USERFILE,
                                                ShareFile.TELEPHONE,
                                                bean.uMobile);
                                        sharedPrefUtil.putString(context,
                                                ShareFile.USERFILE,
                                                ShareFile.USERID, bean.uId);
                                        sharedPrefUtil.putString(context,
                                                ShareFile.USERFILE,
                                                ShareFile.USERNAME,
                                                bean.uNickName);
                                        sharedPrefUtil.putString(context, ShareFile.USERFILE,
                                                ShareFile.PERSONREMARK,
                                                bean.uPersontag);
                                        if (!"".equals(bean.uPortrait)) {
                                            String str = bean.uPortrait
                                                    .toString();
                                            str = str.replace("\\", "");
                                            sharedPrefUtil.putString(context,
                                                    ShareFile.USERFILE,
                                                    ShareFile.USERPHOTOPATH,
                                                    str);
                                        }
                                    } else {
                                        return;
                                    }
                                }
                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                            }
                        } else {
                            return;
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        });
        request.setTag("down");
        request.setRetryPolicy(new DefaultRetryPolicy(10000, 1, 1.0f));
        App.getHttpQueues().add(request);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        App.getHttpQueues().cancelAll("down");
        if (bitmap != null) {
            bitmap.recycle();
        }
        bitmap = null;
    }

    /**
     * 图片加载第一次显示监听器
     *
     * @author Administrator
     */
    private static class AnimateFirstDisplayListener extends
            SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections
                .synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view,
                                      Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                // 是否第一次显示
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    // 图片淡入效果
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }


    boolean autoFag = false;
    private static final int RC_LOCATION_CONTACTS_PERM = 124;

    @AfterPermissionGranted(RC_LOCATION_CONTACTS_PERM)
    private void checkPhonePermission() {
        if (Build.VERSION.SDK_INT < 23) {
            autoFag = true;
        } else {
            String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE};
            if (EasyPermissions.hasPermissions(PersonMessageActivity.this, perms)) {
                // Have permissions, do the thing!
                autoFag = true;
            } else {
                // Ask for both permissions
                EasyPermissions.requestPermissions(this, "该功能需要此权限，为了保证应用正常运行!",
                        RC_LOCATION_CONTACTS_PERM, perms);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // EasyPermissions handles the request result.
        if (PackageManager.PERMISSION_GRANTED == grantResults[0]) {
            autoFag = true;
        } else {
            autoFag = false;
        }
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        Log.d("TAG", "onPermissionsGranted:" + requestCode + ":" + perms.size());
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
// (Optional) Check whether the user denied permissions and checked NEVER ASK AGAIN.
        // This will display a dialog directing them to enable the permission in app settings.
        EasyPermissions.checkDeniedPermissionsNeverAskAgain(this,
                getString(R.string.rationale_ask_again),
                R.string.action_settings, R.string.cancel, null, perms);
    }
}
